package com.project.erp.action.crm.company;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.project.erp.action.BaseAction;
import com.project.erp.common.utils.Page;
import com.project.erp.model.crm.company.Company;
import com.project.erp.service.crm.company.CompanyService;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：CRM-公司信息管理 

 *  功能描述：CRM-公司信息管理 分发处理类

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-12-8 下午4:14:00
 */
@Controller
@Scope("prototype")
public class CompanyAction extends BaseAction implements ModelDriven<Company> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3880100640087708801L;
	
	private Company company;
	private String companyIds;
	private String comId;//用于更新时检测是否已经设置默认显示，因为id用不了
	
	@Autowired
	private CompanyService companyService;

	/**
	 * 取得公司信息列表
	 * @return
	 * @throws Exception
	 */
	public String getCompanyListData() throws Exception{
		logger.debug("entry getCompanyListData method ...");
		try{
			Page<Company> page = new Page<Company>();
			page.setPageNo(getPage());
			page.setPageSize(getRows());
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
			page = companyService.getCompanyPageBySearch(page, company);
			
			//设置表格返回数据
			this.getResultMap().put("total", page.getTotalCount());
			this.getResultMap().put("rows", page.getResult());
		}catch(Exception e){
			logger.error("execute getCompanyListData error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 到添加公司信息页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toAddCompany() throws Exception{
		logger.debug("entry toAddCompany method!...");
		
		return SUCCESS;
	}
	
	/**
	 * 保存公司信息信息
	 * @return SUCCESS
	 * @throws Exception 异常信息
	 */
	public String addCompany() throws Exception{
		logger.debug("entry addCompany method!...");

		try{
			System.out.println("company.getIsDefault()===company.getIsDefault()======"+company.getIsDefault());//////////////////////
			companyService.saveCompany(company);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.add"),this.getText("erp.business.crm.company.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("添加公司信息信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.add"),this.getText("erp.business.crm.company.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 到修改公司信息页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toEditCompany() throws Exception{
		logger.debug("entry toEditCompany method!...");
		try{
			logger.info("********************companyId="+company.getId());
			Long companyId = company.getId();
			company = companyService.getCompanyById(companyId);
			System.out.println("company.getIsDefault()=============="+company.getIsDefault());
		}catch(Exception e){
			e.printStackTrace();
			logger.error("execute toEditCompany page error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 修改公司信息信息
	 * @return
	 * @throws Exception
	 */
	public String editCompany() throws Exception{
		logger.debug("entry editCompany method!...");
		try{
			logger.info("company="+company);
			Company persistentCompany = companyService.getCompanyById(company.getId());
			BeanUtils.copyProperties(company, persistentCompany, new String[]{});
			
			//修改公司信息信息
			companyService.editCompany(persistentCompany);
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.edit"),this.getText("erp.business.crm.company.businame")});
			this.getResultMap().put("msg", msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("修改公司信息信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.edit"),this.getText("erp.business.crm.company.businame")});
			this.getResultMap().put("msg", msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除公司信息
	 * @return
	 * @throws Exception
	 */
	public String delCompany() throws Exception{
		logger.debug("entry delCompany method!...");
		try{
			logger.info("companyIds="+companyIds);
			String[] arrayIds = companyIds.split(",");
			companyService.delCompany(arrayIds);
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.business.crm.company.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			e.printStackTrace();
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.business.crm.company.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 检查是已经存在默认显示的公司信息
	 * @return
	 * @throws Exception
	 */
	public String checkDefaultCompany() throws Exception{
		logger.debug("entry checkDefaultCompany method!...");
		try{
			this.getResultMap().put("checkFlag",true);
			System.out.println("comId======="+comId);///////////////////
			if(comId != null){
				company.setId(Long.parseLong(comId));
			}
			Company comp = companyService.getDefaultCompany();
			
			if(null != comp){
				this.getResultMap().put("checkFlag",false);
			}
		}catch(Exception e){
			//返回错误信息
			logger.error("entry checkDefaultCompany method exception:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
		}
		this.getResultMap().put("result",SUCCESS);
		return SUCCESS;
	}
	
	@Override
	public Company getModel() {
		if(null == company){
			company = new Company();
		}
		return company;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public String getCompanyIds() {
		return companyIds;
	}

	public void setCompanyIds(String companyIds) {
		this.companyIds = companyIds;
	}

	public String getComId() {
		return comId;
	}

	public void setComId(String comId) {
		this.comId = comId;
	}
}
